package org.link.savefile;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.link.util.MainFrame;
import org.link.util.Util;

/**
 * 
 * project_name:ZuoYe
 * package_name:org.link.savefile
 * file_name:SaveFile.java
 * @author Link
 * create_date:2015年8月20日 上午10:44:49
 * modified_date:
 * description: 文件保存窗体
 *
 */
public class SaveFile extends MainFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel checkLabel,saveLabel,targetLabel;
	private JTextField checkField,saveField,targetField;
	private JButton copyButton,cutButton,selectButton1,selectButton2;
	private JFileChooser jfileChooser;
	
	public SaveFile(){
		this.setSize(450, 270);
		Util.screen(this);
		this.setTitle("文件保存");
		this.setForeground(Color.GREEN);
		this.setLayout(null);
		
		checkLabel = new JLabel("目标文件夹位置 : ");
		checkLabel.setFont(new Font("微软雅黑",Font.PLAIN,15));
		checkLabel.setBounds(10, 25, 120, 30);
		this.add(checkLabel);
		
		targetLabel = new JLabel("需要copy的文件类型 : ");
		targetLabel.setFont(new Font("微软雅黑",Font.PLAIN,15));
		targetLabel.setBounds(10, 70, 155, 30);
		this.add(targetLabel);
		
		saveLabel = new JLabel("粘贴到 : ");
		saveLabel.setFont(new Font("微软雅黑",Font.PLAIN,15));
		saveLabel.setBounds(10, 115, 60, 30);
		this.add(saveLabel);
		
		checkField = new JTextField();
		checkField.setFont(new Font("微软雅黑",Font.PLAIN,15));
		checkField.setBounds(140, 25, 200, 30);
		this.add(checkField);
		
		targetField = new JTextField();
		targetField.setFont(new Font("微软雅黑",Font.PLAIN,15));
		targetField.setBounds(170, 70, 170, 30);
		this.add(targetField);
		
		saveField = new JTextField();
		saveField.setFont(new Font("微软雅黑",Font.PLAIN,15));
		saveField.setBounds(90, 115, 250, 30);
		this.add(saveField);
		
		selectButton1 = new JButton("路径选择");
		selectButton1.setFont(new Font("微软雅黑",Font.PLAIN,15));
		selectButton1.setBounds(350, 25, 80, 30);
		selectButton1.setMargin(new Insets(0,0,0,0));
		this.add(selectButton1);
		selectButton1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jfileChooser = new JFileChooser();
				jfileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
				jfileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置选择方式为文件或者文件夹
				int result = jfileChooser.showOpenDialog(null);
				if(JFileChooser.APPROVE_OPTION == result){
					checkField.setText(jfileChooser.getSelectedFile().getAbsolutePath());//将选择的绝对路径给checkField
				}
			}
		});
		
		
		selectButton2 = new JButton("路径选择");
		selectButton2.setFont(new Font("微软雅黑",Font.PLAIN,15));
		selectButton2.setBounds(350, 115, 80, 30);
		selectButton2.setMargin(new Insets(0,0,0,0));
		this.add(selectButton2);
		selectButton2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				
				jfileChooser = new JFileChooser();
				jfileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
				jfileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);//设置选择方式为文件或者文件夹
				int result = jfileChooser.showOpenDialog(null);
				if(JFileChooser.APPROVE_OPTION == result){
					checkField.setText(jfileChooser.getSelectedFile().getAbsolutePath());//将选择的绝对路径给checkField
				}
			}
		});
		
		copyButton = new JButton("复制");
		copyButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		copyButton.setBounds(85, 170, 80, 40);
		copyButton.setMargin(new Insets(0,0,0,0));
		this.add(copyButton);		
		copyButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int result = JOptionPane.showConfirmDialog(null, "确定执行复制操作?","",JOptionPane.YES_NO_OPTION);
				if(result == JOptionPane.NO_OPTION){
					return;
				}
				File saveFile = new File(saveField.getText());
				if(!saveFile.exists()){
					saveFile.mkdir();
				}
				File checkFile = new File(checkField.getText());
				if(checkFile.exists()){
					copyDirectory(checkFile, saveFile,targetField.getText());
				}
			}
		});
		
		cutButton = new JButton("剪切");
		cutButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
		cutButton.setBounds(245, 170, 80, 40);
		cutButton.setMargin(new Insets(0,0,0,0));
		this.add(cutButton);
		cutButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				int result = JOptionPane.showConfirmDialog(null, "确定执行剪切操作?","",JOptionPane.YES_NO_OPTION);
				if(result == JOptionPane.NO_OPTION){
					return;
				}
				File saveFile = new File(saveField.getText());
				if(!saveFile.exists()){
					saveFile.mkdir();
				}
				File checkFile = new File(checkField.getText());
				if(checkFile.exists()){
					cutDirectory(checkFile, saveFile,targetField.getText());
				}
			}
		});
	}
	
	
	/**
	 * 
	 * description: 复制方法体
	 * creatTime:2015年8月20日下午10:32:09
	 * @author Link
	 * return type: void
	 *
	 */
	public static void copyDirectory(File f4,File f5,String targetField){
		
		if(f4.exists()){
			File[] dir = f4.listFiles();
			for (File file2 : dir) {
				if(file2.isDirectory()){//如果是文件夹就继续
					copyDirectory(file2,f5,targetField);
				}else{
					if(file2.getName().endsWith(targetField)){
						File file3 = new File(f5.getPath()+"\\"+file2.getName());
						try {
							FileInputStream input = new FileInputStream(file2);		
							byte[] b = new byte[1024];
							input.read(b);//将文件中的内容读入到byte型的b数组中
							input.close();
							FileOutputStream output = new FileOutputStream(file3);
							output.write(b);//将b数组中的内容存入目标盘中
							output.flush();
							output.close();
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						//file2.renameTo(file3);		
					}
				}
			}
		}	
	}
	
	/*public static void copyDirectory(File f4,File f5,String targetField){
			
		if(f4.exists()){
			File[] dir = f4.listFiles();
			for (File file2 : dir) {
				if(file2.isDirectory()){//如果是文件夹就继续
					copyDirectory(file2,f5,targetField);
				}else{
					if(file2.getName().endsWith(targetField)){
						File file3 = new File(f5.getPath()+"\\"+file2.getName());
						BufferedReader br = null;
						BufferedWriter bw = null;
						FileReader fr = null;
						FileWriter fw = null;	
						try {
							fr = new FileReader(file2);
							br = new BufferedReader(fr);
							String str = null;
							fw = new FileWriter(file3);
							bw = new BufferedWriter(fw);
							while((str = br.readLine())!=null){
								bw.write(str);
							}
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}finally{
							if(br!=null&&fr!=null){
								try {
									br.close();
									fr.close();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							if(bw!=null&&fw!=null){
								try {
									bw.close();
									fw.close();
								} catch (IOException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
							}
						}	
					}
				}
			}
		}	
	}*/
		
	
	
	
	/**
	 * 
	 * description:  剪切方法体
	 * creatTime:2015年8月20日下午10:34:23
	 * @author Link
	 * return type: void
	 *
	 */
	public static void cutDirectory(File f4,File f5,String targetField){
		if(f4.exists()){
			File[] dir = f4.listFiles();
			for (File file2 : dir) {
				if(file2.isDirectory()){//如果是文件夹就递归
					copyDirectory(file2,f5,targetField);
				}else{
					if(file2.getName().endsWith(targetField)){
						File file3 = new File(f5.getPath()+"\\"+file2.getName());
						file2.renameTo(file3);		
					}
				}
			}
		}	
	}
	
	public static void main(String[] args) {
		SaveFile sf = new SaveFile();
		sf.setVisible(true);
	}
}














